using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using AutoMapper;
using HuiLian.Common.Input;
using HuiLian.Common.Output;
using HuiLian.Model.Platform;
using HuiLian.Repository.Platform;
using HuiLian.Service.Platform.CallTime.Input;
//using HuiLian.Service.Platform.CallTime.Output;
using HuiLian.Common.Attributes;
using HuiLian.Common;
using HuiLian.Service.Platform.CallTime.Output;

namespace HuiLian.Service.Platform.CallTime
{
    /// <summary>
    /// 调用时间表服务
    /// </summary>
    public class CallTimeService : ICallTimeService
    {
        private readonly IMapper _mapper;
        private readonly ICallTimeRepository _callTimeRepository;

        public CallTimeService(IMapper mapper, ICallTimeRepository callTimeRepository)
        {
            _mapper = mapper;
            _callTimeRepository = callTimeRepository;
        }

        /// <summary>
        /// 获取调用时间表信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> GetAsync(long id)
        {
            var result = await _callTimeRepository.GetAsync(id);
            return ResponseOutput.Ok(result);
        }

        /// <summary>
        /// 查询调用时间表信息
        /// </summary>
        /// <param name="input">分页信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> PageAsync(PageInput<CallTimeEntity> input)
        {
            //var key = input.Filter?.Label;

            var list = await _callTimeRepository.Select
            .WhereDynamicFilter(input.DynamicFilter)
            //.WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
            .Count(out var total)
            .OrderByDescending(true, c => c.time)
            .Page(input.CurrentPage, input.PageSize)
            .ToListAsync(p => new CallTimeListOutput { time = p.time });

            var data = new PageOutput<CallTimeListOutput>()
            {
                List = list,
                Total = total
            };

            return ResponseOutput.Ok(data);
        }

        /// <summary>
        /// 添加调用时间表信息
        /// </summary>
        /// <param name="input">调用时间表信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> AddAsync(CallTimeAddInput input)
        {
            var entity = _mapper.Map<CallTimeEntity>(input);
            var id = (await _callTimeRepository.InsertAsync(entity)).Id;

            return ResponseOutput.Result(id > 0);
        }

        /// <summary>
        /// 更新调用时间表信息
        /// </summary>
        /// <param name="input">调用时间表信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> UpdateAsync(CallTimeUpdateInput input)
        {
            if (!(input?.Id > 0))
            {
                return ResponseOutput.NotOk();
            }

            var entity = await _callTimeRepository.GetAsync(input.Id);
            if (!(entity?.Id > 0))
            {
                return ResponseOutput.NotOk("数据不存在！");
            }

            _mapper.Map(input, entity);
            await _callTimeRepository.UpdateAsync(entity);
            return ResponseOutput.Ok();
        }

        /// <summary>
        /// 删除调用时间表信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> DeleteAsync(long id)
        {
            var result = false;
            if (id > 0)
            {
                result = (await _callTimeRepository.DeleteAsync(m => m.Id == id)) > 0;
            }

            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 软删除调用时间表信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> SoftDeleteAsync(long id)
        {
            var result = await _callTimeRepository.SoftDeleteAsync(id);
            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 批量软删除调用时间表信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> BatchSoftDeleteAsync(long[] ids)
        {
            var result = await _callTimeRepository.SoftDeleteAsync(ids);

            return ResponseOutput.Result(result);
        }

    }
}
